#include <TimerOne.h> 
#include <EEPROM.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#include "multi_menu.h" 
#include <stdint.h>
#endif
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

//****** IO ******
#define NS  9      
#define SF  10    
#define UP   8    
#define DOWN  5    
#define LEFT   7   
#define RIGHT  6   
#define OK  4      
#define shutter  12     
#define focus  11       
#define DIR 2      
#define STEP 3     
#define EN 13      

//****** EEPROM ******
volatile uint16_t Ex_time = 4;        
volatile uint16_t Interval_time = 2; 
volatile uint16_t Shutter_num = 3;   
volatile uint16_t Shutter_n = Shutter_num;     
volatile uint16_t Rota_angle = 3;    
volatile uint16_t Motor_total_step = 96;  
volatile uint16_t Dece_ratio = 100;       
volatile uint16_t MAX_us = 50;       

uint8_t Ex_time_eeprom = 0;       
uint8_t Interval_time_eeprom = 4; 
uint8_t Shutter_num_eeprom = 8;   
uint8_t Rota_angle_eeprom = 12;   
uint8_t Motor_total_step_eeprom = 16; 
uint8_t Dece_ratio_eeprom = 20;   
uint8_t MAX_us_eeprom = 24;       

//****** EQ ******
//计算：恒星日：23:56:04 = 86164s /减速比/步数/细分数 = 周期，定时器配置=周期/2
//大约的月亮速：89428s 
char *EQ_speedtab_UI[4] = {" ★"," 月","100★"," Max"};  //star moon 100x MAX
float starspeed = 86164*1000/(float)Dece_ratio/(float)Motor_total_step/32*1000;
long EQ_speedtab[4] = {floor(starspeed),floor(starspeed*1.03788),floor(starspeed/100),MAX_us};
uint8_t speedtabi = 0;
char *N_S[2] = {"顺","逆"};


//****** SHUTTER ******
bool ifex = false; //是否在曝光

//****** Gimbal ******
#define focustime 1500      
bool if_pulseend = true;    
long pulseCount = 0;        
unsigned long angle_pulsen; 
unsigned long period_Gimbal;
float angle_p = 0;          

//****** FOCUSER ******
bool outputEnabled = false; 
uint8_t cursor_p = 128;   //屏幕外
uint8_t speedtabi_2 = 0;
uint8_t extime_fc = 0;
char *FC_speedtab_UI[4] = {"1/100","1/50","1/10","Max"};
long FC_speedtab[4] = {100*MAX_us,50*MAX_us,10*MAX_us,MAX_us};
char *Z_F[2] = {"<<<",">>>"};

//****** UI ******
void (*current_operation_index)();
uchar func_index = 0;            
uchar last_index = 100;         
uint16_t current_operation_data;


void setup()
{
  pinMode(NS,  INPUT_PULLUP);  
  pinMode(SF, INPUT_PULLUP); 
  pinMode(UP,  INPUT_PULLUP); 
  pinMode(DOWN, INPUT_PULLUP); 
  pinMode(LEFT, INPUT_PULLUP); 
  pinMode(RIGHT, INPUT_PULLUP); 
  pinMode(OK, INPUT_PULLUP); 
  pinMode(shutter,  OUTPUT);  
  pinMode(focus, OUTPUT);
  pinMode(DIR,  OUTPUT);  
  pinMode(STEP, OUTPUT);
  pinMode(EN,  OUTPUT);  

  /*
  EEPROM.put(Ex_time_eeprom, Ex_time);
  EEPROM.put(Interval_time_eeprom, Interval_time);
  EEPROM.put(Shutter_num_eeprom, Shutter_num);
  EEPROM.put(Rota_angle_eeprom, Rota_angle);
  EEPROM.put(Motor_total_step_eeprom, Motor_total_step);
  EEPROM.put(Dece_ratio_eeprom, Dece_ratio);
  EEPROM.put(MAX_us_eeprom, MAX_us);**/
  
  EEPROM.get(Ex_time_eeprom, Ex_time);
  EEPROM.get(Interval_time_eeprom, Interval_time);
  EEPROM.get(Shutter_num_eeprom, Shutter_num);
  EEPROM.get(Rota_angle_eeprom, Rota_angle);
  EEPROM.get(Motor_total_step_eeprom, Motor_total_step);
  EEPROM.get(Dece_ratio_eeprom, Dece_ratio);
  EEPROM.get(MAX_us_eeprom, MAX_us);

  //定时器初始化
  starspeed = 86164*1000/(float)Dece_ratio/(float)Motor_total_step/32*1000;
  if(floor(starspeed/100)<=MAX_us){
          EQ_speedtab[0] = floor(starspeed);
          EQ_speedtab[1] = floor(starspeed*1.03788);
          EQ_speedtab[2] = MAX_us;
          EQ_speedtab[3] = MAX_us;
          }
        else {
          EQ_speedtab[0] = floor(starspeed);
          EQ_speedtab[1] = floor(starspeed*1.03788);
          EQ_speedtab[2] = floor(starspeed/100);
          EQ_speedtab[3] = MAX_us;
        }
  Timer1.initialize(EQ_speedtab[0]); // 初始化定时器，us
  Timer1.attachInterrupt(motorstep); // 将中断子程序 attach 到定时器

  //电调速度刷新
  FC_speedtab[0] = 100*MAX_us;
  FC_speedtab[1] = 50*MAX_us;
  FC_speedtab[2] = 10*MAX_us;
  FC_speedtab[3] = MAX_us;
  

  //OLED初始化
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.setFont(u8g2_font15_msyh_chinese);
  u8g2.setContrast(1);
  //Serial.begin(9600);
}


void loop() {
  UI_turn();            
  set_period();         
  shutter_EQ_run();     
  shutter_Gimbal_run(); 
  focuser_run();        
}
